package ${package}.service.${moduleName}.impl;

import cn.gooday.jsh.service.common.dto.RestControllerResult;
import cn.gooday.jsh.service.common.exception.JshServerException;
import cn.gooday.jsh.service.common.mybatis.YlhPageInfo;
import cn.gooday.jsh.service.common.util.CheckEmptyUtil;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;
import javax.annotation.Resource;
import ${package}.dao.jpa.${moduleName}.${ClassName}Repository;
import ${package}.dao.mybatis.${moduleName}.${ClassName}Mapper;
import ${package}.dto.api.${moduleName}.Get${ClassName}ParamsDto;
import ${package}.dto.api.${moduleName}.Get${ClassName}ResultDto;
import ${package}.dto.api.${moduleName}.Save${ClassName}ParamsDto;
import ${package}.entity.${moduleName}.${ClassName};
import ${package}.service.${moduleName}.${ClassName}Service;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

/**
 *  ${functionName}.
 */
@Component
public class ${ClassName}ServiceImpl
    implements ${ClassName}Service {

  @Resource
  private ${ClassName}Repository ${ClassName?uncap_first}Repository;

  @Resource
  private ${ClassName}Mapper ${ClassName?uncap_first}Mapper;

  public static final String CONFIG = "${ClassName?uncap_first}";

  @Override
  public YlhPageInfo<Get${ClassName}ResultDto>
        query${ClassName}ListByCondition(
      Get${ClassName}ParamsDto paramsDto) {
    YlhPageInfo<Get${ClassName}ResultDto> ylhPageInfo = new YlhPageInfo<>();
    List<${ClassName}> results =
        ${ClassName?uncap_first}Mapper.query${ClassName}ListByCondition(
            paramsDto, paramsDto.getPageNum(), paramsDto.getPageSize());
    List<Get${ClassName}ResultDto> resultDtos = results.stream()
        .map(this::${ClassName?uncap_first}ToResultDto)
        .collect(Collectors.toList());
    if (CheckEmptyUtil.isNotEmpty(resultDtos)) {
      ylhPageInfo = new YlhPageInfo<>(resultDtos);
    }
    return ylhPageInfo;
  }

  @Override
  public Get${ClassName}ResultDto
          get${ClassName}ById(Integer id) {
    Optional<${ClassName}> configOptional =
        ${ClassName?uncap_first}Repository.findById(id);
    if (configOptional.isPresent()) {
      ${ClassName} ${ClassName?uncap_first} = configOptional.get();
      Get${ClassName}ResultDto ${ClassName?uncap_first}ResultDto =
          ${ClassName?uncap_first}ToResultDto(${ClassName?uncap_first});
      return ${ClassName?uncap_first}ResultDto;
    }
    return null;
  }

  @Override
  public RestControllerResult<Get${ClassName}ResultDto>
          add${ClassName}(
          Save${ClassName}ParamsDto paramsDto) {
    Map<String, Object> resultMap = check${ClassName}(paramsDto);
    if (CheckEmptyUtil.isNotEmpty(resultMap)) {
      ${ClassName} ${ClassName?uncap_first} = (${ClassName}) resultMap.get(CONFIG);
      ${ClassName?uncap_first}Repository.save(${ClassName?uncap_first});
    } else {
      throw new JshServerException("新增失败!");
    }
    return RestControllerResult.success(null);
  }

  @Override
  public RestControllerResult<Get${ClassName}ResultDto>
          update${ClassName}(
          Save${ClassName}ParamsDto paramsDto) {
    Map<String, Object> resultMap = check${ClassName}(paramsDto);
    if (CheckEmptyUtil.isNotEmpty(resultMap)) {
      ${ClassName} ${ClassName?uncap_first} = (${ClassName}) resultMap.get(CONFIG);
      ${ClassName?uncap_first}Repository.save(${ClassName?uncap_first});
    } else {
      throw new JshServerException("修改失败!");
    }
    return RestControllerResult.success(null);
  }

  @Override
  public RestControllerResult<Boolean>
          delete${ClassName}ById(Integer id) {
    RestControllerResult<Boolean> result = new RestControllerResult();
    result.setData(
        ${ClassName?uncap_first}Mapper
              .deleteByPrimaryKey(id));
    return result;
  }

  /**
   * 检查必传入参是否没传值.
   */
  public Map<String, Object>
          check${ClassName}(Save${ClassName}ParamsDto paramsDto) {
    ${ClassName} ${ClassName?uncap_first} = new ${ClassName}();
    Integer id = paramsDto.getId();
    final Map<String, Object> resultMap = new HashMap<>(2);
    if (CheckEmptyUtil.isNotEmpty(id)) {
      Optional<${ClassName}> configOptional =
          ${ClassName?uncap_first}Repository
              .findById(id);
      if (configOptional.isPresent()) {
        ${ClassName?uncap_first} = configOptional.get();
        ${ClassName?uncap_first}.setId(paramsDto.getId());
      }
    }
  <#list fieldList as field>
    <#if field.attrName != 'createTime' &&
      field.attrName != 'updateTime'&&
      field.attrName != 'updateUserId'&&
      field.attrName != 'createUserId'&&
      field.attrName != 'isDelete'&&
      field.attrName != 'version'&&
      field.attrName != 'id'
      >
    <#if field.attrType == 'String' && field?has_content>
    if (CheckEmptyUtil.isNotEmpty(paramsDto.get${field.attrName?cap_first}())) {
      ${ClassName?uncap_first}.set${field.attrName?cap_first}(paramsDto.get${field.attrName?cap_first}());
    } else {
      throw new JshServerException("缺少${field.fieldComment}");
    }
    <#else>
    if (CheckEmptyUtil.isNotEmpty(paramsDto.get${field.attrName?cap_first}())) {
      ${ClassName?uncap_first}.set${field.attrName?cap_first}(paramsDto.get${field.attrName?cap_first}());
    } else {
      throw new JshServerException("缺少${field.fieldComment}");
    }
    </#if>
    </#if>
  </#list>
    resultMap.put(CONFIG, ${ClassName?uncap_first});
    return resultMap;
  }

  /**
   * 将${ClassName}转换为Get${ClassName}ResultDto.
   */
  public Get${ClassName}ResultDto
          ${ClassName?uncap_first}ToResultDto(
          ${ClassName} ${ClassName?uncap_first}) {
    Get${ClassName}ResultDto resultDto = new Get${ClassName}ResultDto();
    BeanUtils.copyProperties(${ClassName?uncap_first}, resultDto);
    return resultDto;
  }

  @Override
  public Integer change${ClassName}StatusById(
      Integer id, Integer status) {
    return ${className}Mapper.changeStatusById(id, status);
  }
}
